package com.asa.ui.frame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.asa.ui.spirit.Circle;
/**
 * 这个不怎么用java的控件，都是自己画在JPanel上的
 * @author asa
 *
 */
public class AlgoFrame extends JFrame{
	
	public AlgoFrame(String title,int canvasWidth,int canvasHeight) {
		super(title);
		this.canvasWidth = canvasWidth;
		this.canvasHeight = canvasHeight;
		
		AlgoCanvas canvas = new AlgoCanvas();
//		canvas.setPreferredSize(new Dimension(canvasWidth,canvasHeight));
		
//		this.setSize(canvasWidth,canvasHeight);
		this.setContentPane(canvas);
		pack();
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}
	public int getCanvasWidth() {
		return canvasWidth;
	}
	public int getCanvasHeight() {
		return canvasHeight;
	}
	private int canvasWidth;
	private int canvasHeight;
	private Circle[] circles;
	public void render(Circle[] circles) {
		
		this.circles = circles;
		this.repaint();
	}
	
	private class AlgoCanvas extends JPanel{
		
		@Override
		public void paintComponent(Graphics g) {
			
			super.paintComponent(g);
			
			Graphics2D g2d = (Graphics2D) g;
			
			//抗锯齿
			RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
			g2d.addRenderingHints(hints);
			
			//双缓存	默认已经支持了
			
			
			
			//具体绘制
//			AlgoVisHelper.setStrokeWidth(g2d, 5);
//			AlgoVisHelper.setColor(g2d,Color.BLUE);
//			AlgoVisHelper.fillCircle(g2d, canvasWidth/2, canvasHeight/2, 200);
//			AlgoVisHelper.setColor(g2d,Color.RED);
//			AlgoVisHelper.strokeCircle(g2d, canvasWidth/2, canvasHeight/2, 200);
			
			
			
			AlgoVisHelper.setStrokeWidth(g2d, 1);
			AlgoVisHelper.setColor(g2d,Color.RED);
//			for (int i = 0; i < circles.length; i++) {
//				Circle circle = circles[i];
//				AlgoVisHelper.strokeCircle(g2d, circle.x, circle.y, circle.getR());
//				
//			}
			
			
			for (int i = 0; i < circles.length; i++) {
				Circle circle = circles[i];
				//如果填充属性为假则描边否则填充
				if(!circle.isFilled){
					AlgoVisHelper.strokeCircle(g2d,circle.x,circle.y,circle.getR());
				}else{
					AlgoVisHelper.fillCircle(g2d,circle.x,circle.y,circle.getR());
				}

			
		}
			
			
			
		}
		
		
		@Override
		public Dimension getPreferredSize() {
			
			return new Dimension(canvasWidth, canvasHeight);
		}
		
	}
	
	
	
}
